<template>
  <div class="login-container">
    <section>
      <!-- 背景色 -->
      <div class="color" />
      <div class="color" />
      <div class="color" />
      <div class="box">
        <!-- 背景圆 -->
        <div class="circlre" style="--x:0" />
        <div class="circlre" style="--x:1" />
        <div class="circlre" style="--x:2" />
        <div class="circlre" style="--x:3" />
        <div class="circlre" style="--x:4" />
        <!-- 登录框 -->
        <div class="container">
          <el-form ref="regionForm" :model="regionForm" :rules="regionRules" class="login-form form" auto-complete="on" label-position="left">
            <h2>注册</h2>
            <!-- <div class="title-container">
              <h3 class="title">Login Form</h3>
            </div> -->
            <el-form-item prop="nickname" class="inputBox">
              <span class="svg-container">
                <svg-icon icon-class="dashboard" />
              </span>
              <el-input
                ref="nickname"
                v-model="regionForm.nickname"
                placeholder="Nickname"
                name="nickname"
                type="text"
                auto-complete="on"
              />
            </el-form-item>
            <el-form-item prop="username" class="inputBox">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="regionForm.username"
                placeholder="Username"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>

            <el-form-item prop="password" class="inputBox">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="regionForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>

            <el-button
              class="inputBox myButton"
              :loading="loading"
              type="primary"
              style="width:100%;margin-bottom:30px;height:30px"
              @click.native.prevent="handleRegion"
            >注册</el-button>

            <!-- <div class="tips">
              <span style="margin-right:20px;">username: admin</span>
              <span> password: any</span>
            </div> -->
            <p class="forget">
              已有账户?
              <a href="#" @click.prevent="gotoLogin">点击登录</a>
            </p>
          </el-form>
        </div>
      </div>
    </section>

  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Region',
  data() {
    // 校验规则
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    const validateNickname = (rule, value, callback) => {
      if (value.length < 5) {
        console.log(1)
        callback(new Error('Please enter the correct nickname'))
      } else {
        callback()
      }
    }
    return {
      regionForm: {
        username: 'admin',
        password: '111111',
        nickname: 'ggboy'
      },
      regionRules: {
        username: [{ required: true, trigger: 'blur', message: '账户不能为空', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空', validator: validatePassword }],
        nickname: [{ required: true, trigger: 'blur', message: '昵称不能为空', validator: validateNickname }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleRegion() {
      this.$refs.regionForm.validate(valid => {
        if (valid) {
          console.log(1)
          this.loading = true
          this.$store.dispatch('user/region', this.regionForm).then(() => {
            console.log(5)
            this.$router.push({ path: this.redirect || '/login' })
            this.$message({
              message: '注册成功',
              type: 'success'
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    gotoLogin() {
      this.$router.push({
        name: 'Login'
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
/* 清除浏览器默认边距 */
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
/* 水平垂直居中 */
section{
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  background: linear-gradient(to bottom,#f1f4f9,#dff1ff);
  // 背景颜色
  .color{
    position: absolute;
    filter: blur(200px);
    &:nth-child(1){
      top:-350px;
      width: 600px;
      height: 600px;
      background: #ff359d;
    }
    &:nth-child(2){
      bottom: -150px;
      left: 100px;
      width: 500px;
      height: 500px;
      background: #fffd87;
    }
     &:nth-child(3){
      bottom: 50px;
      right: 100px;
      width: 500px;
      height: 500px;
      background: #00d2ff;
    }
  }
  // 登录框+圆
  .box{
    position: relative;
    // 圆
    .circlre{
      position: absolute;
      background: rgba(255,255,255,.1);
      backdrop-filter: blur(5px);
      box-shadow: 0 25px 45px rgba(0,0,0,.1);
      border:1px solid rgba(255,255,255,.5);
      border-right: rgba(255,255,255,.1);
      border-bottom: rgba(255,255,255,.1);
      // 给图像应用色相旋转
      filter: hue-rotate(calc(var(--x)*80deg));
      animation: animate 10s linear infinite;
      animation-delay: calc(var(--x)*-2s);
      border-radius: 50%;
      &:nth-child(1){
        top: -50px;
        right: -60px;
        width: 100px;
        height: 100px;
      }
      &:nth-child(2){
        top: 150px;
        left: -100px;
        width: 120px;
        height: 120px;
        z-index: 2;
      }
      &:nth-child(3){
        bottom: 50px;
        right: -60px;
        width: 80px;
        height: 80px;
        z-index: 2;
      }
       &:nth-child(4){
        bottom: -80px;
        left: 100px;
        width: 60px;
        height: 60px;
        z-index: 2;
      }
       &:nth-child(5){
        top: -80px;
        left: 140px;
        width: 60px;
        height: 60px;
         z-index: 2;
      }
    }
    @keyframes animate{
      0%,100%{
        transform: translate(-50px);
      }
      50%{
        transform: translate(50px);
      }
    }
    // 登录框
    .container{
      position: relative;
      width: 400px;
      min-height: 400px;
      background: rgba(255,255,255,.1);
      display: flex;
      justify-content: center;
      align-items: center;
      backdrop-filter: blur(5px);
      box-shadow: 0 25px 45px rgba(0, 0, 0, .1);
      border:1px solid rgba(255,255,255,.5);
      border-right: 1px solid rgba(255,255,255,.1);
      border-bottom: 1px solid rgba(255,255,255,.1);
    }
    .form{
      position: relative;
      width: 100%;
      height: 100%;
      padding: 50px;
      // 登录框标题样式
      h2{
        position: relative;
        color:#fff;
        font-size: 24px;
        font-weight: 600;
        letter-spacing: 5px;
        margin-bottom: 30px;
        cursor: pointer;
         // 登录框下划线
        &::before{
          content: "";
          position: absolute;
          bottom: -5px;
          width: 1\0px;
          height: 3px;
          background: #fff;
          transition: .5s;
        }
        &:hover{
          &::before{
            width: 53px;
          }
        }
      }
      // 输入框样式
      .inputBox{
        width: 100%;
        margin-top: 20px;
        input {
          width: 100%;
          padding: 10px 20px;
          background: rgba(255,255,255,.3);
          outline: none;
          border: none;
          border-radius: 30px;
          border: 1px solid rgba(255,255,255,.5);
          border-right: 1px solid rgba(255,255,255,.1);
          border-bottom: 1px solid rgba(255,255,255,.1);
          font-size: 16px;
          letter-spacing: 1px;
          color:#fff;
          box-shadow: 0 5px 15px rgba(0, 0, 0, .05);
          &::placeholder{
            color: #fff;
          }
        }
      }
      // .myButton{
      //   background: #fff;
      //   color: #666;
      //   max-width: 120px;
      //   margin-bottom: 20px;
      //   font-weight: 600;
      //   cursor: pointer;
      // }
      // 忘记密码样式
      .forget{
        margin-top: 5px;
        color: #fff;
        letter-spacing: 1px;
        a{
          font-weight: 600;
          text-decoration: none;
          color: #fff;
        }
      }
    }
  }
}
</style>
